package com.itdct.onflow.generator.template

import com.itdct.onflow.generator.base.BaseTemplate
import com.itdct.onflow.generator.config.GeneratorConfig
import com.itdct.onflow.generator.constant.GroupEnum
import com.itdct.onflow.generator.model.ClassModel
import com.itdct.onflow.generator.model.CompareResultModel
import com.itdct.onflow.generator.model.FieldModel

/**
 * @author Zhouwx
 * @date 2025/8/12 15:50:41
 * @version 1.0
 * @description
 */
class MapperJavaTemplate(classModel: ClassModel, generatorConfig: GeneratorConfig) : BaseTemplate(classModel, generatorConfig) {
    override fun render(): String {
        return """
${uuidPrint()}
package ${classModel.packageName}.mapper;

import com.itdct.onflow.core.mapper.BaseMapper;
import ${classModel.packageName}.entity.${classModel.className}Entity;
import ${classModel.packageName}.qo.${classModel.className}SearchQo;
import ${classModel.packageName}.vo.${classModel.className}DetailVo;
import ${classModel.packageName}.vo.${classModel.className}ListVo;

import org.apache.ibatis.annotations.Mapper;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;

import java.util.List;${extraImports()}

/**
 * @author ${generatorConfig.authorName}
 * @date ${getDateString()}
 * @version 1.0
 * @description ${classModel.classUsage}查询工具
 */
@Mapper
public interface ${classModel.className}Mapper extends BaseMapper<${classModel.className}Entity> {
    @Nullable
    ${classModel.className}DetailVo findById(Long id);

    @NotNull
    List<${classModel.className}ListVo> findByList(${classModel.className}SearchQo qo);

${rangeStart("selectFunctions")}
${selectFunctions()}
${rangeEnd("selectFunctions")}

}

        """.trimIndent()
    }

    override fun onModify(compareResultModel: CompareResultModel, content: String): String {
        var content = content
        val addForSelectFields = compareResultModel.addForSelect
        val addIndex = content.indexOf(rangeEnd("selectFunctions"))
        content = insertFieldListContent(content, addForSelectFields, addIndex, { index, fieldModel -> selectFunctionContent(fieldModel) })

        val delForSelect = compareResultModel.delForSelect
        content = deleteRangeFunctionCode(content, "selectFunctions", delForSelect, { fieldModel -> selectFunctionContent(fieldModel) })

        return content
    }

    private fun selectFunctions(): String {
        val stringBuilder = StringBuilder()
        classModel.fieldList.filter {
            it.forSelect
        }.forEach {
            stringBuilder.append(
                selectFunctionContent(it)
            )
        }
        return stringBuilder.toString()
    }

    private fun selectFunctionContent(it: FieldModel) = """
|    @NotNull
|    List<KeyLabelValueVo> select${it.nameBigCamel}List(${classModel.className}SearchQo qo);
|    
                """.trimMargin()

    private fun extraImports(): String {
        val stringBuilder = StringBuilder()
        classModel.fieldList.any {
            it.forSelect
        }.also {
            if (it) {
                stringBuilder.append("\nimport com.itdct.onflow.core.vo.KeyLabelValueVo;")
            }
        }
        return stringBuilder.toString()
    }

    override fun belongGroup(): GroupEnum {
        return GroupEnum.BACKEND
    }

    override fun subModuleName(): String {
        return "adaptor"
    }

    override fun getFolderFileName(): String {
        return "mapper/${classModel.className}Mapper.java"
    }
}